package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname RoleServiceImpl
 * @Description TODO
 * @Date 2020/8/27 8:30 下午
 * @Created by apple
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;

    // 新增角色
    public void add(Role role) {
        roleDao.add(role);
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<Role> list = roleDao.findByCondition(queryString);
        PageInfo pageInfo = new PageInfo<>(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    // 根据id查询
    public Role findById(Integer id) {
        Role role = roleDao.findByRoleId(id);
        return role;
    }

    // 根据角色id查询对应的权限
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        List<Integer> list = roleDao.findPermissionIdsByRoleId(id);
        return list;
    }

    // 修该权限
    public void editPermission(Integer[] permissionIds, Integer roleId) {
        roleDao.deleteAssociation(roleId);
        for (Integer permissionId : permissionIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("permissionId", permissionId);
            map.put("roleId", roleId);
            roleDao.setPermissionAndRole(map);
        }
    }

    // 删除角色
    public void delete(Integer id) {
        List<Integer> list = roleDao.findPermissionIdsByRoleId(id);
        if (list != null && list.size() > 0) {
            roleDao.deleteAssociation(id);
        }
        roleDao.delete(id);
    }

    @Override
    //查找所有菜单
    public List<Menu> findMenuAll() {
        List<Menu> list =menuDao.findAll();
        return list;
    }

    @Override
    //根据角色ID查询当前角色包含的菜单ID
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        List<Integer> listid= roleDao.findMenuIdsByRoleId(id);
        return listid;
    }

    @Override
    //修改角色绑定的菜单
    public void editMenu(List<Integer> listid,Integer roleId) {
        //删除外键关系
        roleDao.deleteRole_MenuNull(roleId);
        for (Integer integer : listid) {
            //根据id查询父id
           Integer parentid=roleDao.findParentIdByMenuId(integer);
            //新增角色绑定的菜单
            roleDao.addRole_Menu(roleId,integer);


        }

    }
}
